package com.hean.view;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

import com.hean.entity.Cell;
import com.hean.entity.Worm;

public class WormView extends View {
	private Worm worm;

	private int maxLeft;
	private int maxUp;
	private int maxRight;
	private int maxBottom;

	public WormView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void setWorm(Worm worm) {
		this.worm = worm;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.rgb(50, 50, 200));
		if (worm != null) {
			int width = getWidth();
			int height = getHeight();

			maxLeft = (width - worm.getMaxCellX() * Cell.SIZE) / 2;
			maxUp = (height - worm.getMaxCellY() * Cell.SIZE) / 2;

			maxRight = maxLeft + worm.getMaxCellX() * Cell.SIZE;
			maxBottom = maxUp + worm.getMaxCellY() * Cell.SIZE;

			Paint paint = new Paint();

			// 
			paint.setColor(Color.rgb(100, 100, 255));
			canvas.drawRect(maxLeft, maxUp, maxRight, maxBottom, paint);
			//
			drawWrom(canvas, worm);
		}

	}

	private void drawWrom(Canvas canvas, Worm worm) {
		List<Cell> cells = worm.getCells();
		Paint paint = new Paint();
		for (int i = 0; i < cells.size(); i++) {
			
			Cell cell = cells.get(i);
			int x = cell.x * Cell.SIZE + maxLeft;
			int y = cell.y * Cell.SIZE + maxUp;
			//画内部颜色
			paint.setColor(Color.rgb(255, 255, 0));
			canvas.drawRect(x, y, x + Cell.SIZE, y + Cell.SIZE, paint);
			//画黑边
			paint.setStyle(Style.STROKE);
			paint.setColor(Color.rgb(0, 0, 0));
			canvas.drawRect(x, y, x + Cell.SIZE, y + Cell.SIZE, paint);
		}
	}
}
